package com.jjwxc.jwjskandriod.widget;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;

public class TimeGroupPop {
    static String[] time={"2023","2022","2021","2020","2019","2018"};

    public static void selectTimeGroupPop(View v, Activity context, PopDismissListener popDismissListener) {
        View view = LayoutInflater.from(context).inflate(R.layout.time_group_pop, null, false);
        LinearLayout linearLayout = view.findViewById(R.id.ll_views);
        View view_view = view.findViewById(R.id.view_view);
        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.ffProg_anim);
        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        view_view.setOnClickListener(v12 -> {
            popWindow.dismiss();
        });

        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
            }
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);


        for (int i = 0; i < time.length; i++) {
            View viewone = LayoutInflater.from(context).inflate(R.layout.edit_group_pop_item, null);
            ImageView img_more = viewone.findViewById(R.id.img_more);
            TextView tv_numbook = viewone.findViewById(R.id.tv_numbook);
            TextView tv_title = viewone.findViewById(R.id.tv_title);
            TextView tv_line = viewone.findViewById(R.id.tv_line);

                tv_line.setVisibility(View.VISIBLE);
            img_more.setVisibility(View.GONE);
            tv_numbook.setVisibility(View.GONE);

            tv_title.setText(time[i]);


            int finalI = i;
            viewone.setOnClickListener(v1 -> {
                popDismissListener.onOkClick(time[finalI]);
                popWindow.dismiss();
            });
            linearLayout.addView(viewone);
        }
    }
    public interface PopDismissListener {
        void onOkClick(String time);
    }
}
